﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Game.Foundation.ChineseChess
{
    public class Shuai : ChessmanBase
    {
        public Shuai(Chessboard board, Camp camp, string text) : base(board, camp, text) { }

        public override bool CanMoveTo(int xLocation, int yLocation)
        {
            if ((xLocation >= 4 && xLocation <= 6) && ((yLocation >= 1 && yLocation <= 3) || (yLocation >= 8 && yLocation <= 10)))
            {
                int xoffset = xLocation - this.XLocation;
                int yoffset = yLocation - this.YLocation;
                if (xoffset * yoffset == 0 && Math.Abs(xoffset + yoffset) == 1)
                {
                    return true;
                }

                //飞将
                if (xoffset == 0)
                {
                    Shuai dest = this._board.GetChessman(xLocation, yLocation) as Shuai;
                    if (dest != null && dest.Camp != this.Camp)
                    {
                        int count = 0;
                        for (int y = this.YLocation + yoffset / Math.Abs(yoffset); y != yLocation; y += yoffset / Math.Abs(yoffset))
                        {
                            if (this._board.GetChessman(this.XLocation, y) != null) count++;
                        }

                        return count == 0;
                    }
                }
            }

            return false;
        }
    }
}
